<?php
/**
 * 自定义异常类
 *
 */

abstract class MyException extends Exception
{
	var $tpl;

	function __construct($message,$code)
	{
		global $tpl,$sys_conf_var;

		if($sys_conf_var['system']['error'] === false)
		{
			header("HTTP/1.1 404 Not Found");
			header("Status: 404 Not Found");
			exit;
		}

		parent::__construct($message,$code);

		if(!is_object($tpl))
		{
			$tpl = new Compiler();
			$tpl->compile_dir  = CACHE_DIR  . 'compile/';
		}

		$this->tpl = $tpl;
		$this->tpl->template_dir = SYS_PATH  . 'template/error/';
	}

	abstract function showError();

	function __destruct()
	{
		exit;
	}
}

class MySqlException extends MyException
{
	/**
	 * 得到异常错误信息
	 *
	 * @return string
	 */
	function getError()
	{
		//error message
		$errorMsg = 'Error number : <b>' . mysql_errno() . '</b><br>Error message : <b>' . mysql_error() . '</b>';
		return $errorMsg;
	}

	/**
	 * 得到异常错误请求语句
	 *
	 * @return string
	 */
	function getQuery()
	{
		//error message
		if($this->getMessage())
		{
			$errorMsg =  'Error query : <b>' . $this->getMessage() . '</b>';
			return $errorMsg;
		}
	}

	/**
	 * 显示错误
	 *
	 */
	function showError()
	{
		$this->tpl->assign('Error',$this->getError());
		$this->tpl->assign('Query',$this->getQuery());

		$this->tpl->display('error_db.html');
	}
}

class AppException extends MyException
{
	/**
	 * 显示错误
	 *
	 */
	function showError()
	{
		$this->tpl->assign('Error','Error : <b>' . $this->getMessage() . '</b>');

		$this->tpl->display('error_app.html');
	}
}
?>